package com.kingdee.eas.custom.hostelmgr.beddistribution.app;

import org.apache.log4j.Logger;

import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
import com.kingdee.bos.dao.IObjectPK;
import com.kingdee.bos.dao.IObjectValue;
import com.kingdee.bos.dao.ormapping.ObjectStringPK;
import com.kingdee.bos.dao.ormapping.ObjectUuidPK;
import com.kingdee.bos.metadata.entity.EntityViewInfo;
import com.kingdee.bos.metadata.entity.FilterInfo;
import com.kingdee.bos.metadata.entity.FilterItemInfo;
import com.kingdee.bos.metadata.query.util.CompareType;
import com.kingdee.eas.basedata.person.PersonCollection;
import com.kingdee.eas.basedata.person.PersonFactory;
import com.kingdee.eas.basedata.person.PersonInfo;
import com.kingdee.eas.common.EASBizException;
import com.kingdee.eas.custom.hostelmgr.basedata.DistributionOrNotEnum;
import com.kingdee.eas.custom.hostelmgr.basedata.HostelBasicInfo;
import com.kingdee.eas.custom.hostelmgr.basedata.NeedRoomEnum;
import com.kingdee.eas.custom.hostelmgr.beddistribution.BedDistributionCollection;
import com.kingdee.eas.custom.hostelmgr.beddistribution.BedDistributionFactory;
import com.kingdee.eas.custom.hostelmgr.beddistribution.BedDistributionInfo;
import com.kingdee.eas.framework.CoreBaseCollection;
import com.kingdee.eas.hr.emp.PersonContactMethodCollection;
import com.kingdee.eas.hr.emp.PersonContactMethodFactory;
import com.kingdee.eas.hr.emp.PersonContactMethodInfo;
import com.kingdee.eas.util.app.DbUtil;

public class BedDistributionControllerBean extends
		AbstractBedDistributionControllerBean {
	private static Logger logger = Logger
			.getLogger("com.kingdee.eas.custom.hostelmgr.beddistribution.app.BedDistributionControllerBean");

	protected IObjectPK _submit(Context ctx, IObjectValue model)
			throws BOSException, EASBizException {

		BedDistributionInfo bedDestinationInfo = (BedDistributionInfo) model;

		CoreBaseCollection coreBaseCollection = new CoreBaseCollection();
		PersonInfo newPerson = bedDestinationInfo.getPerson();
		PersonInfo oldPerson = this.getPersonByBed(ctx, bedDestinationInfo);

		EntityViewInfo param = new EntityViewInfo();
		FilterInfo filter = new FilterInfo();
		filter.getFilterItems().add(
				new FilterItemInfo("person.id", newPerson.getId().toString(),
						CompareType.EQUALS));
		param.setFilter(filter);

		String sql = "UPDATE T_HR_PERSONCONTACTMETHOD SET FADDRESS=? WHERE FPERSONID=?";

		if (oldPerson != null) {
			oldPerson.setIsDistribution(DistributionOrNotEnum.NO);
			oldPerson.setBed(null);
			coreBaseCollection.add(oldPerson);
		}

		if (newPerson != null) {
			HostelBasicInfo bed = newPerson.getBed();
			if (bed != null) {
				BedDistributionCollection distCollection = BedDistributionFactory
						.getLocalInstance(ctx).getBedDistributionCollection(
								param);
				if (distCollection.size() > 0) {
					BedDistributionInfo distInfo = distCollection.get(0);
					distInfo.setPerson(null);
					distInfo.setMobile(null);
					distInfo.setRemark(null);
					distInfo.setStayDate(null);
					BedDistributionFactory.getLocalInstance(ctx).update(
							new ObjectUuidPK(distInfo.getId()), distInfo);
				}
			}
			newPerson.setIsDistribution(DistributionOrNotEnum.YES);
			newPerson.setBed(bedDestinationInfo.getBed());
			coreBaseCollection.add(newPerson);
		}

		PersonFactory.getLocalInstance(ctx).update(coreBaseCollection);

		if (oldPerson != null) {
			DbUtil.execute(ctx, sql, new Object[] { "",
					oldPerson.getId().toString() });
		}
		if (newPerson != null) {
			DbUtil.execute(ctx, sql, new Object[] {
					bedDestinationInfo.getBed().getDisplayName(),
					newPerson.getId().toString() });
		}

		return super._submit(ctx, model);
	}

	protected void _clear(Context ctx, IObjectPK[] distributions,
			IObjectPK[] persons) throws BOSException, EASBizException {
		BedDistributionInfo bedDistributionInfo = null;
		PersonInfo personInfo = null;

		CoreBaseCollection personCollection = new CoreBaseCollection();
		CoreBaseCollection bedDistributionCollection = new CoreBaseCollection();

		for (int i = 0; i < persons.length; i++) {
			if (persons[i].getKeyValue("id") == null) {
				continue;
			} else {
				bedDistributionInfo = this.getBedDistributionInfo(ctx,
						distributions[i]);
				if (bedDistributionInfo != null) {
					bedDistributionInfo.setPerson(null);
					bedDistributionInfo.setMobile(null);
					bedDistributionInfo.setRemark(null);
					bedDistributionInfo.setStayDate(null);
					bedDistributionCollection.add(bedDistributionInfo);
				}

				personInfo = PersonFactory.getLocalInstance(ctx).getPersonInfo(
						persons[i]);
				if (personInfo != null) {
					personInfo.setBed(null);
					personInfo.setIsDistribution(DistributionOrNotEnum.NO);
					personInfo.setIsRoom(NeedRoomEnum.NO);
					personCollection.add(personInfo);
				}
			}
		}
		this.update(ctx, bedDistributionCollection);
		PersonFactory.getLocalInstance(ctx).update(personCollection);
	}

	protected PersonInfo getPersonByBed(Context ctx,
			BedDistributionInfo bedDestinationInfo) throws BOSException {
		EntityViewInfo params = new EntityViewInfo();
		FilterInfo filter = new FilterInfo();
		filter.getFilterItems().add(
				new FilterItemInfo("bed", bedDestinationInfo.getBed().getId(),
						CompareType.EQUALS));
		params.setFilter(filter);
		PersonCollection personCollection = PersonFactory.getLocalInstance(ctx)
				.getPersonCollection(params);
		if (personCollection.size() > 0) {
			return personCollection.get(0);
		}
		return null;
	}
}